<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>管理用户</title>
    <style>
        div {
            position: absolute;
            top: 30%;
            left: 50%;
            transform: translate(-50%, -50%);
            text-align: center;
        }

        table, tr, th, td {
            border: 1px solid #5fbdcb;
        }

        table {
            border-collapse: collapse;
        }
    </style>
</head>
<body>
<div>
    <table></table>
    <br>
    <a href="BanUser.jsp">封禁的用户</a>
    <a href="ManagerPage.jsp">返回</a>
</div>
<script src="https://lib.sinaapp.com/js/jquery/2.0.2/jquery-2.0.2.min.js"></script>
<script>
    $(function () {
        $.ajax({
            url: "http://localhost:8080/user.do",
            type: "post",
            data: {
                method: 'selectAll',
            },
            dataType: "text",
            success: function (data) {
                let parse = JSON.parse(data);
                if (parse.length === 0) {
                    alert("还没有用户！")
                    window.location.href = 'ManagerPage.jsp';
                }
                let table = document.querySelector('table');
                for (let i = 0; i < parse.length; i++) {
                    let tr = document.createElement('tr');
                    table.appendChild(tr);
                    let th1 = document.createElement('th');
                    th1.innerText = '用户名：';
                    tr.appendChild(th1);
                    let td1 = document.createElement('td');
                    td1.innerText = parse[i].userName;
                    tr.appendChild(td1);
                    let th2 = document.createElement('th');
                    th2.innerText = '邮箱：';
                    tr.appendChild(th2);
                    let td2 = document.createElement('td');
                    td2.innerText = parse[i].email;
                    tr.appendChild(td2);
                    let th3 = document.createElement('th');
                    th3.innerText = '身份：';
                    tr.appendChild(th3);
                    let td3 = document.createElement('td');
                    if (parse[i].identity === 1) {
                        td3.innerText = '用户';
                    }
                    if (parse[i].identity === 2) {
                        td3.innerText = '商家';
                    }
                    tr.appendChild(td3);
                    let td4 = document.createElement('td');
                    tr.appendChild(td4);
                    let a = document.createElement('a');
                    a.innerText = '封禁';
                    a.href = 'DoBan.jsp?userId=' + parse[i].userId;
                    td4.appendChild(a);
                }
            }
        })
    })
</script>
</body>
</html>
